package com._21cn._front.cms.api;

import com._21cn.cms.article.entity.Article;
import com._21cn.cms.article.entity.ArticleThumbPicture;
import com._21cn.cms.article.entity.ColumnArticle;
import com._21cn.cms.article.entity.RegionArticle;
import com._21cn.cms.article.service.ArticleService;
import com._21cn.cms.article.service.ArticleThumbPictureService;
import com._21cn.cms.article.service.ColumnArticleService;
import com._21cn.cms.article.service.RegionArticleService;
import com._21cn.cms.common.utils.ResponseUtil;
import com._21cn.cms.site.entity.Column;
import com._21cn.cms.site.service.ColumnService;
import com._21cn.framework.flip.PageList;
import com._21cn.framework.http.HttpRequestInfo;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/10.
 */
@Controller
@RequestMapping({"/judian/api"})
public class JudianApiController {

    private final static Integer channelId = 6;
    private final static Integer flagYes = 1;

    public static final int PUBLISH_STATUS_NONE = 0;            //未发布
    public static final int PUBLISH_STATUS_DONE = 1;            //人工发布
    public static final int PUBLISH_STATUS_FETCH_DONE = 2;     //采集发布
    public static final int PUBLISH_STATUS_CANCEL = 4;          //撤稿

    public static final int IMAGE_TYPE_FIRST_PIC    =   1;
    public static final int IMAGE_TYPE_3_PIC        =   2;

    @Autowired
    private ColumnService columnService;

    @Autowired
    private ColumnArticleService columnArticleService;

    @Autowired
    private RegionArticleService regionArticleService;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private ArticleThumbPictureService articleThumbPictureService;

    @RequestMapping({"/category/recommend.do"})
    public void categoryRecommend(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        String appVersion = reqInfo.getParameter("app_version","");
        String devicePlatform = reqInfo.getParameter("device_platform","");

        Map<String, Object> params = new HashMap();
        params.put("eqChannelId", channelId);
        params.put("eqStatus", flagYes);
        params.put("eqIsShow", flagYes);
        List<Column> list = columnService.getList(params, 1, 100, true);
        Map<String, Object> resultMap = new HashMap();
        resultMap.put("count", list.size());
        List itemList = new ArrayList();
        for(Column column: list){
            JSONObject object = new JSONObject();
            object.put("cate_id", column.getId());
            object.put("cate_name", column.getName());
            object.put("description", column.getMetaDescription());
            itemList.add(object);
        }
        resultMap.put("items", itemList);
        String responseResult = JSONObject.toJSONString(resultMap, new SerializerFeature[] { SerializerFeature.WriteDateUseDateFormat });
        ResponseUtil.returnJsonMsg(response, responseResult);
    }

    @RequestMapping({"/article/list.do"})
    public void articleList(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        String appVersion = reqInfo.getParameter("app_version","");
        String devicePlatform = reqInfo.getParameter("device_platform","");
        Integer columnId = reqInfo.getIntParameter("cate_id",0);
        Integer pageNo = reqInfo.getIntParameter("step",1);
        Integer pageSize = 10;
        //预留
        String uid = reqInfo.getParameter("uid","");
        String requestTime = reqInfo.getParameter("request_time","");

        List<Integer> inStatusList = new ArrayList<Integer>();
        inStatusList.add(PUBLISH_STATUS_DONE);
        inStatusList.add(PUBLISH_STATUS_FETCH_DONE);

        Map<String, Object> params = new HashMap();
        params.put("inPublishStatusList", inStatusList);
        params.put("eqColumnId", columnId);
        params.put("orderByClause", "TOP_TIME DESC, PUBLISH_TIME DESC");
        PageList<ColumnArticle> list = columnArticleService.getList(params, pageNo, pageSize, true);

        Map<String, Object> resultMap = new HashMap();
        List itemList = new ArrayList();
        for(ColumnArticle columnArticle: list){
            JSONObject object = new JSONObject();
            object.put("article_id", columnArticle.getArticleId());
            object.put("article_title", columnArticle.getTitle());
            object.put("thumb_url", columnArticle.getFirstPicUrl());
            object.put("article_url", columnArticle.getArticleAppUrl());
            object.put("video_url", "");                //预留
            object.put("cate_id", columnArticle.getColumnId());
            object.put("publish_time", columnArticle.getPublishTime());
            object.put("like_num", 0);                  //预留
            object.put("read_num", 0);                  //预留
            object.put("share_num", 0);                 //预留
            object.put("tagid", "");                    //预留
            object.put("tagname", "");                 //预留
            object.put("account_id", 0);                 //预留
            Article article = articleService.getById(columnArticle.getArticleId(), false);
            object.put("account_name", article.getSourceName());
            object.put("account_avatar", "");                 //预留
            object.put("article_type", columnArticle.getArticleType());
            object.put("title_color", "");
            //3张图
            Map<String, Object> tempParam = new HashMap();
            tempParam.put("eqArticleId", columnArticle.getArticleId());
            List<ArticleThumbPicture> thumbPictures = articleThumbPictureService.getList(tempParam, 1, 3, false);
            if(thumbPictures!=null && thumbPictures.size()==3){
                object.put("image_type", IMAGE_TYPE_3_PIC);
                object.put("thumb_url", thumbPictures.get(0).getPicUrl());
                object.put("thumb_extra1", thumbPictures.get(1).getPicUrl());
                object.put("thumb_extra2", thumbPictures.get(2).getPicUrl());
            }else {
                object.put("image_type", IMAGE_TYPE_FIRST_PIC);
            }
            itemList.add(object);
        }
        resultMap.put("count", list.getPageTurn().getRowCount());
        resultMap.put("step", pageNo);
        resultMap.put("items", itemList);
        String responseResult = JSONObject.toJSONString(resultMap, new SerializerFeature[] { SerializerFeature.WriteDateUseDateFormat });
        ResponseUtil.returnJsonMsg(response, responseResult);
    }

    @RequestMapping({"/article/regionList.do"})
    public void articleHotList(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        String appVersion = reqInfo.getParameter("app_version","");
        String devicePlatform = reqInfo.getParameter("device_platform","");
        Integer regionId = reqInfo.getIntParameter("region_id",0);
        Integer pageNo = reqInfo.getIntParameter("step",1);
        Integer pageSize = 10;
        //预留
        String uid = reqInfo.getParameter("uid","");
        String requestTime = reqInfo.getParameter("request_time","");
        Integer orderby = reqInfo.getIntParameter("orderby",1);

        List<Integer> inStatusList = new ArrayList<Integer>();
        inStatusList.add(PUBLISH_STATUS_DONE);
        inStatusList.add(PUBLISH_STATUS_FETCH_DONE);

        Map<String, Object> params = new HashMap();
        params.put("inPublishStatusList", inStatusList);
        params.put("eqRegionId", regionId);
        params.put("orderByClause", "TOP_TIME DESC, PUBLISH_TIME DESC");
        PageList<RegionArticle> list = regionArticleService.getList(params, pageNo, pageSize, true);

        Map<String, Object> resultMap = new HashMap();
        List itemList = new ArrayList();
        for(RegionArticle regionArticle: list){
            JSONObject object = new JSONObject();
            object.put("article_id", regionArticle.getArticleId());
            object.put("article_title", regionArticle.getTitle());
            object.put("thumb_url", regionArticle.getFirstPicUrl());
            object.put("article_url", regionArticle.getArticleAppUrl());
            object.put("video_url", "");                //预留
            object.put("region_id", regionArticle.getRegionId());
            object.put("publish_time", regionArticle.getPublishTime());
            object.put("like_num", 0);                  //预留
            object.put("read_num", 0);                  //预留
            object.put("share_num", 0);                 //预留
            object.put("tagid", "");                    //预留
            object.put("tagname", "");                 //预留
            object.put("account_id", 0);                 //预留
            Article article = articleService.getById(regionArticle.getArticleId(), false);
            object.put("account_name", article.getSourceName());
            object.put("account_avatar", "");                 //预留
            object.put("article_type", regionArticle.getArticleType());
            object.put("title_color", "");
            //3张图
            Map<String, Object> tempParam = new HashMap();
            tempParam.put("eqArticleId", regionArticle.getArticleId());
            List<ArticleThumbPicture> thumbPictures = articleThumbPictureService.getList(tempParam, 1, 3, false);
            if(thumbPictures!=null && thumbPictures.size()==3){
                object.put("image_type", IMAGE_TYPE_3_PIC);
                object.put("thumb_url", thumbPictures.get(0).getPicUrl());
                object.put("thumb_extra1", thumbPictures.get(1).getPicUrl());
                object.put("thumb_extra2", thumbPictures.get(2).getPicUrl());
            }else {
                object.put("image_type", IMAGE_TYPE_FIRST_PIC);
            }
            itemList.add(object);
        }
        resultMap.put("count", list.getPageTurn().getRowCount());
        resultMap.put("step", pageNo);
        resultMap.put("items", itemList);
        String responseResult = JSONObject.toJSONString(resultMap, new SerializerFeature[] { SerializerFeature.WriteDateUseDateFormat });
        ResponseUtil.returnJsonMsg(response, responseResult);
    }

    @RequestMapping({"/version/update.do"})
    public void versionUpdate(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer appId = reqInfo.getIntParameter("app_id",0);
        String appPlatform = reqInfo.getParameter("app_platform","");
        String versionCode = reqInfo.getParameter("version_code","");
        String versionName = reqInfo.getParameter("version_name","");

        Map<String, Object> resultMap = new HashMap();
        resultMap.put("version_code", "1");
        resultMap.put("version_name", "");
        resultMap.put("app_platform", "");
        resultMap.put("force_upgrade", false);
        resultMap.put("min_force_upgrade_version_code", "");
        resultMap.put("download_url", "");
        resultMap.put("file_size", 0);
        resultMap.put("version_memo", "test");

        String responseResult = JSONObject.toJSONString(resultMap, new SerializerFeature[] { SerializerFeature.WriteDateUseDateFormat });
        ResponseUtil.returnJsonMsg(response, responseResult);
    }

}
